package cn.edu.besti.is.onlinecalculator;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * Created by littlecurl 2018/6/24
 */
public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {
    private DBOpenHelper mDBOpenHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        Button reg = findViewById(R.id.reg);
        reg.setOnClickListener(this);
        mDBOpenHelper = new DBOpenHelper(this);

    }

    public void onClick(View view) {
        EditText user = findViewById(R.id.ed_user2);
        EditText pwd = findViewById(R.id.ed_pwd2);
        EditText pwdConf = findViewById(R.id.ed_pwd3);
        //注册按钮
        if (view.getId() == R.id.reg) {//获取用户输入的用户名、密码、验证码
            String username = user.getText().toString().trim();
            String password = pwd.getText().toString().trim();
            String passwordConf = pwdConf.getText().toString();
            //注册验证
            if (!TextUtils.isEmpty(username) && !TextUtils.isEmpty(password) && !TextUtils.isEmpty(passwordConf)) {
                if (password.equals(passwordConf)) {
                    //将用户名和密码加入到数据库中
                    mDBOpenHelper.add(username, password);
                    Intent intent2 = new Intent(this, LoginActivity.class);
                    startActivity(intent2);
                    finish();
                    Toast.makeText(this, "验证通过，注册成功", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(this, "两次输入密码不同,注册失败", Toast.LENGTH_SHORT).show();
                }
            } else {
                Toast.makeText(this, "未完善信息，注册失败", Toast.LENGTH_SHORT).show();
            }
        }
    }
}
